//
//  FFWalletRechargeVM.m
//  LLWallet
//
//  Created by tt on 2020/8/17.
//

#import "FFWalletRechargeVM.h"
#import "FFRechargeInputCell.h"
#import "FFPayTypeItemCell.h"
#import "WKButtonItemCell.h"
#import "WKButtonItemCell2.h"
#import "FFRechargeMoneyGridCell.h"


@interface FFWalletRechargeVM ()

@property(nonatomic,assign) CGFloat rechargeMoney; // 充值金额

@property(nonatomic,strong) FFRechargeConfig *config; // 充值配置

@end

@implementation FFWalletRechargeVM



- (NSArray<NSDictionary *> *)tableSectionMaps {
    if(!self.config) {
        return nil;
    }
    NSMutableArray *payTypeItemDicts = [NSMutableArray array];
//    NSArray *payTypeItemModels =  [self getPayTypeItemModels];
    __weak typeof(self) weakSelf = self;
//    if(payTypeItemModels && payTypeItemModels.count>0) {
//        for (FFPayTypeItemModel *model in payTypeItemModels) {
//            [payTypeItemDicts addObject:@{
//                @"class": FFPayTypeItemModel.class,
//                @"sid": model.sid?:@"",
//                @"title":model.title?:@"",
//                @"icon":model.icon,
//                @"selected": model.selected,
//                @"onClick":^{
//                    weakSelf.paySID = model.sid;
//                    if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(ff_walletRechargePayTypeSelected)]) {
//                        [weakSelf.delegate ff_walletRechargePayTypeSelected];
//                    }
//                }
//            }];
//        }
//    }
 return @[
     @{
          @"height":@(20.0f),
          @"remark":[NSString stringWithFormat:LLang(@"当前钱包余额%@元"),[WKMoneyUtil penny2YuanFormat:@(self.config.walletAmount)]],
          @"items":@[
                  @{
                      @"class":FFRechargeInputModel.class,
                      @"showBottomLine":@(NO),
                      @"showTopLine":@(NO),
                      @"value": @(weakSelf.rechargeMoney),
                      @"onChange":^(NSString *value){
                          self.rechargeMoney = value.floatValue;
                          if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(ff_walletRechargeMoneyChange:)]) {
                              [weakSelf.delegate ff_walletRechargeMoneyChange:value.floatValue];
                          }
                      }
                  },
          ]
     },
     @{
           @"height":@(0.01f),
           @"items":@[
                   @{
                       @"class": FFRechargeMoneyGridModel.class,
                       @"value": @(weakSelf.rechargeMoney),
                       @"items":[self getRechargeAmounts],
                       @"onItemClick":^(FFRechargeMoney *money){
                           weakSelf.rechargeMoney = money.money.floatValue;
                           if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(ff_walletRechargeMoneyClick:)]) {
                               [weakSelf.delegate ff_walletRechargeMoneyClick:money];
                           }
                       }
                   }
           ],
     },
     @{
          @"height":@(0.01f),
          @"items":payTypeItemDicts,
     },
     @{
          @"height":@(40.0f),
          @"items":@[
                  @{
                      @"class":WKButtonItemModel2.class,
                      @"title": LLang(@"下一步"),
                      @"onPressed":^{
                          if(weakSelf.rechargeMoney<=0) {
                            [[WKNavigationManager shared].topViewController.view showHUD:LLangW(@"充值金额不能等于或小于0！", weakSelf)];
                            return;
                          }
                          if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(ff_walletRecharge)]) {
                              [weakSelf.delegate ff_walletRecharge];
                          }
                      }
                  },
          ]
     }
 ];
}

// 请求数据
- (void)requestData:(void (^)(NSError * _Nullable))complete {
    __weak typeof(self) weakSelf = self;
    [[WKAPIClient sharedClient] GET:@"pay/rechargeconfig" parameters:nil model:FFRechargeConfig.class].then(^(FFRechargeConfig *config){
        weakSelf.config = config;
        complete(nil);
    }).catch(^(NSError *error){
        complete(error);
    });
}

// 充值可选金额
-(NSArray<FFRechargeMoney*>*) getRechargeAmounts {
    NSMutableArray<FFRechargeMoney*> *items = [NSMutableArray array];
    if(self.config.amounts) {
        for (NSNumber *amount in self.config.amounts) {
            [items addObject:[FFRechargeMoney initWith:[NSString stringWithFormat:@"¥%0.0f",[WKMoneyUtil penny2Yuan:amount].floatValue] money:[WKMoneyUtil penny2Yuan:amount]]];
        }
    }
    return items;
}

-(AnyPromise*) recharge {
   return [[WKAPIClient sharedClient] POST:@"pay/recharge" parameters:@{
        @"amount": [WKMoneyUtil yuan2Penny:@(self.rechargeMoney)],
        @"pay_type": @(self.payType),
    } model:FFPrepayResult.class];
}




-(NSArray<FFPayTypeItemModel*>*) getPayTypeItemModels {
    NSMutableArray *payTypeItems = [NSMutableArray array];
    if(self.config.channels) {
        NSInteger i = 0;
        for (FFRechargeChannel *channel in self.config.channels) {
            NSString *paySid = [NSString stringWithFormat:@"%ld",channel.type];
            bool selected = false;
            if(i==0 && (!self.paySID || [self.paySID isEqualToString:@""])) {
                self.paySID = paySid;
            }
            if(self.paySID && [self.paySID isEqualToString:paySid]) {
                selected = true;
            }
            NSData *icon = [[NSData alloc] initWithBase64EncodedString:channel.icon options:NSDataBase64DecodingIgnoreUnknownCharacters];
            [payTypeItems addObject:[FFPayTypeItemModel initWithSID:paySid title:channel.title?:@"" icon:icon selected:selected]];
            i++;
        }
    }
    return payTypeItems;
}

@end

@implementation FFRechargeConfig

+ (WKModel *)fromMap:(NSDictionary *)dictory type:(ModelMapType)type {
    FFRechargeConfig *config = [FFRechargeConfig new];
    config.amounts = dictory[@"amounts"];
    config.walletAmount = [dictory[@"wallet_amount"] integerValue];
    NSArray *channelDicts = dictory[@"channels"];
    
    NSMutableArray *channels = [NSMutableArray array];
    if(channelDicts && channelDicts.count>0) {
        for (NSDictionary *channelDict in channelDicts) {
            [channels addObject:(FFRechargeChannel*)[FFRechargeChannel fromMap:channelDict type:type]];
        }
    }
    config.channels = channels;
    return config;
}

@end

@implementation FFRechargeChannel

+ (WKModel *)fromMap:(NSDictionary *)dictory type:(ModelMapType)type {
    FFRechargeChannel *channel = [FFRechargeChannel new];
    channel.title = dictory[@"title"];
    channel.icon = dictory[@"icon"];
    channel.type = [dictory[@"type"] integerValue];
    return channel;
}

@end


